package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;

import problem.logic;
import utils.AstarStrategy;
import utils.GreedyStrategy;
import utils.ShowGraphGui;
import agents.Agent;
import agents.Greedy;
import agents.Human;
import agents.Search;
import agents.TerroristBuster;
import env.Move;
import env.World;

public class Main {
	// constants
	public static final int noOpCost = 1, hellBreakLooseCost = 1000,cutoff = 100;

	/**
	 * @param args
	 */
	public static void maintest(String[] args) {

		World world = utils.Parser.parseWorldFile("world.txt");
		World world2 = new World(world);
		world2.addAgent(new Greedy(world2, world2.getCityByName("1"), null),world.getCityByName("1"));
		ShowGraphGui.showGraph(world,"world1");
		ShowGraphGui.showGraph(world2,"world2");
	}
	public static void main(String[] args) {

		World world = utils.Parser.parseWorldFile("world.txt");
		int cost = 0;
		World copiedWorld = null;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line;
		System.out.print("how many agents do you wish to make?:");
		try {
			line = br.readLine();
			int numRead = Integer.parseInt(line);
			for (int i = 0; i < numRead; i++) {
				System.out
						.println("type of agent\n\t1:human\n\t2:greedy\n\t3:terrorist buster\n\t4:greedy search\n\t5:A*search\n\t6:real time A* search");
				line = br.readLine();
				int agentType = Integer.parseInt(line);
				System.out.println("type initial city of agent");
				line = br.readLine();
				String initCity = new String(line);
				System.out.println("type goal city of agent");
				line = br.readLine();
				String goalCity = new String(line);
				Agent nAgent = null;
				
				switch (agentType) {
				case 1:
					nAgent = new Human(world, world.getCityByName(initCity),
							world.getCityByName(goalCity));
					world.addAgent(nAgent,world.getCityByName(initCity));
					break;
				case 2:
					nAgent = new Greedy(world, world.getCityByName(initCity),
							world.getCityByName(goalCity));
					world.addAgent(nAgent,world.getCityByName(initCity));
					break;
				case 3:
					nAgent = new TerroristBuster(world,
							world.getCityByName(initCity),
							world.getCityByName(goalCity));
					world.addAgent(nAgent,world.getCityByName(initCity));
					break;
				case 4:
					copiedWorld = new World(world);
					nAgent = new Search(world,
							copiedWorld,
							world.getCityByName(initCity),
							world.getCityByName(goalCity),
							new GreedyStrategy(copiedWorld.getCityByName(goalCity)),
							0);
					world.addAgent(nAgent,world.getCityByName(initCity));
				case 5:
					copiedWorld = new World(world);
					nAgent = new Search(world,
							copiedWorld,
							world.getCityByName(initCity),
							world.getCityByName(goalCity),
							new AstarStrategy(copiedWorld.getCityByName(goalCity)),
							0);
					world.addAgent(nAgent,world.getCityByName(initCity));
					break;
				case 6:
					copiedWorld = new World(world);
					nAgent = new Search(world,
							copiedWorld,
							world.getCityByName(initCity),
							world.getCityByName(goalCity),
							new AstarStrategy(copiedWorld.getCityByName(goalCity)),
							cutoff);
					world.addAgent(nAgent,world.getCityByName(initCity));
					break;
				}
				//world.getCityByName(initCity).addAgent(nAgent);
		
				

				}
			int counter = 0;
			utils.ShowGraphGui.showGraph(world, "world");
			while (!world.allDone()) {
				Iterator<Agent> it = world.getAgents().iterator();
				
				
				while (it.hasNext()) {
					Agent agent = it.next();
					if (!agent.isDone()) {
						Move move = agent.calcMove();
						
						if (move != null) {
							System.out.println(move);
							world.moveAgents(move);
							counter++;
							utils.ShowGraphGui.showGraph(world, "world"+counter);
						}
						
						//else	System.out.println("no op");
						cost += logic.calcCost(move);
					}
				
				}
				

			}
			System.out.println("All done, cost:" + cost);
		} catch (IOException e) {

			e.printStackTrace();
		}

		/*
		 * while (br != null) { world.show();
		 * 
		 * try { line = br.readLine();
		 * 
		 * // while (line != null) {
		 * 
		 * utils.Parser.parseLine(line); // line = br.readLine();
		 * 
		 * } catch (IOException e) { // Auto-generated catch block
		 * e.printStackTrace(); } } try { br.close(); } catch (IOException e) {
		 * // Auto-generated catch block e.printStackTrace(); } }
		 */
	}
}
